package com.darkplanet.management.dao;

import com.darkplanet.management.domain.Model;
import com.darkplanet.management.util.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class ModelDao implements IModelDao {
    QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDs());

    @Override
    public Model getUserById(Integer model_id) {
        Model ModelUser = null;
        String sql = "select * from model where model_id = ?";
        try {
            ModelUser = queryRunner.query(sql,new BeanHandler<Model>(Model.class),model_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ModelUser;
    }

    @Override
    public void addModelUser(Integer requirement_id,String model_name,String priority,String simple_desc,String comlplex_desc,String remark) {
        String sql = "insert into model(requirement_id,model_name,priority,simple_desc,comlplex_desc,remark) values(?,?,?,?,?,?)";
        try {
            queryRunner.update(sql,requirement_id,model_name,priority,simple_desc,comlplex_desc,remark);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteModelUser(Integer model_id) {
        String sql = "delete from model where model_id = ?";
        try {
            queryRunner.update(sql,model_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void updateModelUser(Integer requirement_id,String model_name,String priority,String simple_desc,String comlplex_desc,String remark,Integer model_id) {
        String sql = "update model set requirement_id = ?,model_name = ?,priority = ?,simple_desc = ?,comlplex_desc = ?,remark = ? where model_id = ?";
        try {
            queryRunner.update(sql,requirement_id,model_name,priority,simple_desc,comlplex_desc,remark,model_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List getModelUser() {
        List getModelUser = null;
        String sql = "select * from model";
        try {
            getModelUser = queryRunner.query(sql,new BeanListHandler<Model>(Model.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return getModelUser;
    }

    @Override
    public Model getmodel_id(String model_name) {
        Model ModelUser = null;
        String sql = "select * from model where model_name = ?";
        try {
            ModelUser = queryRunner.query(sql,new BeanHandler<Model>(Model.class),model_name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ModelUser;
    }
}
